using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

namespace FrameWork.Inventory
{
    public class ShopDescription : SingletonMonoBehaviour<ShopDescription>
    {
        [Header("物品介绍栏")]
        [SerializeField] private TMP_Text itemName;
        [SerializeField] private TMP_Text itemDescription;
        
        [Header("按钮")]
        [SerializeField] private Button buy;
        [SerializeField] private Button sell;
        
        //
        private ItemSO _item;

        protected override void Awake()
        {
            base.Awake();
            
            buy.onClick.AddListener(() =>
            {
                // 物品不为空
                if (_item != null)
                {
                    // 获取金币数量，看是否能够购买该物品
                    if (Player.Instance.GetCurrentConisState(_item.ItemPrice))
                    {
                        // 消费金币，传入负数
                        print(_item.ItemName + _item.ItemPrice);
                        EventSystem.Instance.EventTrigger(EventName.EVENT_PLAYER_INFORMATION_UPDATE_COINS, -(float)_item.ItemPrice);
                        // 添加物品
                        EventSystem.Instance.EventTrigger(EventName.EVENT_INVENTORY_MODEL_ADDITEM, _item, 1);
                    }
                    else
                    {
                        Debug.Log("金币数量不够，金币剩余" + Player.Instance.GetPlayerCoins() + "还需要金币" + (_item.ItemPrice - Player.Instance.GetPlayerCoins()));
                    }
                }
                else
                {
                    Debug.LogError("没有选择购买的商品");
                }
            });
            
            sell.onClick.AddListener(() =>
            {
                // 物品不为空
                if (_item != null)
                {
                    // 出售商品，移除
                    EventSystem.Instance.EventTrigger(EventName.EVENT_INVENTORY_MODEL_REMOVEITEM, _item, 1);
                    // 增加金币，售价的八折
                    EventSystem.Instance.EventTrigger(EventName.EVENT_PLAYER_INFORMATION_UPDATE_COINS,
                        (_item.ItemPrice - 2));
                }
                else
                {
                    Debug.LogError("没有选择购买的商品");
                }
            });
        }

        /// <summary>
        /// 每次打开均刷新页面
        /// </summary>
        private void OnEnable()
        {
            _item = null;
            itemName.text = "商品名称";
            itemDescription.text = "商品介绍";
        }

        public void SetDescription(ItemSO item)
        {
            _item = item;
            itemName.text = item.ItemName;
            itemDescription.text = item.ItemDescription;
        }
    }
}
